home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’96 / ABC Hack / source / SaneNumerics.c < prev    next >
Text File  |  1996-06-22  |  3KB  |  134 lines

  1. // SaneNumerics        6/21/96
  2. // by Nathan Tennies, John Gotow, and Jeff
  3. //
  4. // SaneNumerics builds a code resource that can be appended to an 'itl2' resource to change the normal
  5. // sort ordering of text through the Internation Utilities.  SaneNumerics sorts numbers embedded in
  6. // text in the natural way, with '3' coming before '21', rather than alphabetically.  SaneNumerics can
  7. // be changed - by changing the ComparePartialStrings function, to sort text in other ways.
  8.  
  9. #include "SaneNumerics.h"
  10.  
  11. void main (void) {
  12.     asm {
  13.         bra        Init
  14.         bra        Fetch
  15.         dc.l    'NSrt'        // OSType identifier
  16.         dc.l    0            // OldInit            offset 8
  17.         dc.l    0            // OldFetch            offset 12
  18.         dc.l    0            // OldResourceSize    offset    16
  19.     }
  20. }
  21.  
  22. void Init (void) {
  23.     asm {
  24.         move.w    #0, STACKOFFSET(IUSortFrame, oldA6, flag) (a6);
  25.         
  26.         lea        main,a0
  27.         move.l    20(a0),d1
  28.         move.l    12(a0), d0
  29.         sub.l    d1,a0
  30.         add.l    d0,a0
  31.         jsr        (a0)
  32.     }
  33. }
  34.  
  35. void Fetch (void) {
  36.     asm {
  37.         tst.w    STACKOFFSET(IUSortFrame, oldA6, flag)(a6)
  38.         bne        @secondStr
  39.         
  40.         move.l    IUStrData.strPtr (a2), STACKOFFSET(IUSortFrame, oldA6, remStrA) (a6)
  41.         move.w    IUStrData.strCnt (a2), STACKOFFSET(IUSortFrame, oldA6, remLenA) (a6)
  42.         move.w    #1, STACKOFFSET(IUSortFrame, oldA6, flag) (a6)
  43.         
  44.         lea        main,a0
  45.         move.l    20(a0),d1
  46.         move.l    16(a0), d0
  47.         sub.l    d1,a0
  48.         add.l    d0,a0
  49.         jsr        (a0)
  50.         rts
  51.  
  52.         @secondStr:
  53.         
  54.         move.w    IUStrData.strCnt (a2), d0
  55.         add.w    #1, d0
  56.         move.w    d0,-(a7)
  57.         
  58.         move.w    STACKOFFSET(IUSortFrame, oldA6, remLenA) (a6), d0
  59.         add.w    #1, d0
  60.         move.w    d0,-(a7)
  61.         
  62.         move.l    IUStrData.strPtr (a2), -(a7)
  63.         move.l    STACKOFFSET(IUSortFrame, oldA6, remStrA) (a6), -(a7)
  64.         
  65.         lea        STACKOFFSET(IUSortFrame, oldA6, remStrA) (a6), a0
  66.         move.l    a0, -(a7)
  67.             
  68.         lea        main,a0
  69.         move.l    20(a0),d1
  70.         move.l    16(a0), d0
  71.         sub.l    d1,a0
  72.         add.l    d0,a0
  73.         jsr        (a0)
  74.  
  75.         jsr        ComparePartialStrings
  76.         add.l    #16,a7
  77.             
  78.         tst.w    d0
  79.         beq        @oldFetch
  80.         
  81.         cmp.w    #1, d0
  82.         beq        @strAGreater
  83.         
  84.         move.w    #0xFF00, d4
  85.         rts
  86.         
  87.         @strAGreater:
  88.         
  89.         move.w    #0x0000, d4
  90.         rts
  91.         
  92.         @oldFetch:
  93.  
  94.         lea        main,a0
  95.         move.l    20(a0),d1
  96.         move.l    16(a0), d0
  97.         sub.l    d1,a0
  98.         add.l    d0,a0
  99.         jsr        (a0)
  100.  
  101.         rts
  102.     }
  103. }
  104.  
  105. short RunLen (char *ptr, short remainingLen) {
  106.     short runLen = 0;
  107.     while ((remainingLen > 0) && (*ptr >= '0') && (*ptr <= '9')) {
  108.         ++runLen;
  109.         ++ptr;
  110.         --remainingLen;
  111.     }
  112.  
  113.     return runLen;
  114. }
  115.  
  116. // ComparePartialStrings accepts two partial strings, remStrA and remStrB, of lengths remLenA and remLenB.
  117. // This function should return 1 if remStrA is greater than remStrB, -1 if remStrA is less than remStrB,
  118. // and 0 if no changes to the default sort order are to be made.  sortFrame contains additional information,
  119. // including the whole size of the strings.
  120. short ComparePartialStrings (IUSortFrame *sortFrame, char * remStrA, char * remStrB, short remLenA, short remLenB) {
  121.     short compareResult = 0;
  122.     
  123.     short digitRunLenA = RunLen (remStrA, remLenA);
  124.     short digitRunLenB = RunLen (remStrB, remLenB);
  125.  
  126.     if ((digitRunLenA > 0) && (digitRunLenB > 0) && (digitRunLenA != digitRunLenB)) {
  127.         if (digitRunLenA < digitRunLenB)
  128.             compareResult = -1;
  129.         else
  130.             compareResult = 1;
  131.     }
  132.     
  133.     return compareResult;
  134. }